package shopping;

import shopping.Cart;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author zheng
 */
public class CartDaoJTableImpl implements CartDao{
    
    private JTable jTable;

    public CartDaoJTableImpl(JTable jTable) {
        this.jTable = jTable;
    }
    
    
    
    @Override
    public boolean save(Cart cart) {
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();
        String[] strs = convertCartToStringArr(cart);
        model.addRow(strs);
        return true;
    }

    private String[] convertCartToStringArr(Cart cart) {
       String[] strs =new String[3];
       strs[0] = cart.getName();
       strs[1] = cart.getPrice()+"";
       strs[2] = cart.getNum()+"";
       return strs;
    }

    @Override
    public boolean deleteByIndex(int index) {
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();        
        model.removeRow(index);
        return true;
    }
    
}

